package com.minxi.jsp.dao.impl;

import com.minxi.jsp.dao.UserDao;
import com.minxi.jsp.javabean.UserInfo;
import com.mysql.jdbc.Driver;
import com.sun.org.apache.xpath.internal.operations.Bool;

import java.rmi.server.ExportException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class UserDaoImpl implements UserDao {
    @Override
    public Boolean add(UserInfo userInfo) {
        Boolean flag = false;
        // 操作数据库的方法  JDBC
        Connection c = null;
        PreparedStatement p = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/student?serverTimezone=Asia/Shanghai";
            String name = "root";
            String pwd = "654321";
            c = DriverManager.getConnection(url, name, pwd);
            String sql = "insert into user_info (user_name, user_pwd, user_phone, user_sex) values (?,?,?,?)";
            p = c.prepareStatement(sql);
            p.setString(1, userInfo.getUser_name());
            p.setString(2, userInfo.getUser_pwd());
            p.setString(3, userInfo.getUser_phone());
            p.setString(4, userInfo.getUser_gender());
            int i = p.executeUpdate();
            if (i > 0) flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                p.close();
                c.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return flag;
    }
}
